import axios from 'axios'
import config from '@/../config'
import { ElMessage } from 'element-plus'
import { userModule } from '@/store/modules/user'
// axios.defaults.withCredentials = true
const service = axios.create({
  baseURL: config.baseApi,
  timeout: 1000 * 5
})
service.interceptors.request.use(config => {
  // 请求 token
  config.headers.token = userModule.token
  return config
})

service.interceptors.response.use(response => {
  const data = response.data
  if (response.status === 200) {
    if (data.code !== 200) {
      ElMessage({
        message: data.msg || '未知错误',
        type: 'error'
      })
      return Promise.reject(data)
    } else return Promise.resolve(data)
  } else return Promise.reject(data)
}, error => {
  ElMessage({
    message: error.message || '未知错误',
    type: 'error'
  })
})
export default service
